﻿using Microsoft.Extensions.Options;
using System;
using System.Data;
using System.Data.SqlClient;

namespace orders.Repository
{
    public interface IBaseRepository<T> where T : class, new()
    {

    }


    public interface IDataSource
    {
         IDbConnection GetConnection();
    }

    public class DataSource: IDataSource
    {
        private readonly IDbConnection _connection;
        public DataSource(IOptions<DapperOptions> options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            var options1 = options.Value;

            _connection = new SqlConnection { ConnectionString = options1.ConnectionString };
            _connection.Open();
        }

        public IDbConnection GetConnection()
        {
            return _connection;
        }
    }

    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        protected readonly IDbConnection _connection;
        public BaseRepository(IDataSource dataSource)
        {
            _connection = dataSource.GetConnection();
        }


        // todo basic CRUD
    }
}
